<!-- 组织结构 首页 -->
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索栏-->
      <div v-if="crud.props.searchToggle">
        <el-row :gutter="20">
          <el-col :xs="8" :sm="7" :md="6" :lg="4">
            <dep-tree :dep-input-change="depSearchInput" :load-on-mounted="true" />
          </el-col>
          <el-col :xs="16" :sm="17" :md="18" :lg="20">
            <rrOperation />
          </el-col>
        </el-row>

      </div>
      <!-- 按钮-->
      <crud-opts :permission="permission" />
    </div>
    <!-- 表格-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      border
      style="width: 100% "
      row-key="id"
      stripe
      lazy
      :load="loadChildren"
      :tree-props="{children: 'children', hasChildren: 'hasChildren' }"
      tooltip-effect="dark"
      :highlight-current-row="true"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @sort-change="crud.sortChange"
      @selection-change="crud.selectionChangeHandler"
      @row-click="clickLine"
    >
      <el-table-column type="selection" width="39" />
      <!--<el-table-column type="index" width="45" />-->

      <el-table-column prop="depName" label="组织名字" sortable="custom" />

      <el-table-column prop="depCode" :show-overflow-tooltip="true" label="组织机构代码" sortable="custom" />
      <el-table-column
        prop="status"
        label="状态"
        width="80"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            class="switch"
            :width="50"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="启用"
            inactive-text="禁用"
            :disabled="!permission.has('edit')"
            @change="crud.changeStatusWithNum(scope.row, scope.row.status, {'title':'组织','column':'depName'})"
          />
        </template>
      </el-table-column>
      <el-table-column prop="paixu" label="排序" width="80" header-align="center" align="center" sortable="custom" />
      <!-- <el-table-column prop="remark" label="备注" sortable="custom" />-->

      <el-table-column prop="createUserName" label="创建人" :show-overflow-tooltip="true" />
      <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true" header-align="center" sortable="custom" />
      <el-table-column fixed="right" label="操作" width="120" header-align="center" align="center">
        <template slot-scope="scope">
          <!-- 操作按钮，简介操作的（不需要详情和扩展的），可以只引用 udOperation -->
          <udOperation
            :data="scope.row"
            :permission="permission"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件, 如果不需要注释掉下面一行,以及引入的 pagination 组件即可 -->
    <!-- <pagination />-->
    <!-- 表单 -->
    <SysDepartmentForm />
    <!-- 详情, 如果用不到，注释下面一行 -->
    <SysDepartmentInfo />
  </div>
</template>
<!-- 组织结构 首页 -->
<script>
import crudApi, { list } from '@/api/sys/Department'
import SysDepartmentForm from './form'
import SysDepartmentInfo from './info'
import CRUD, { presenter, header } from '@crud/crud'
import CrudOpts from '@crud/CRUD.operation'
import rrOperation from '@crud/RR.operation'
// import udOperation from '@crud/UD.operation'
import udOperation from '@crud/RUD.operation'
import DepTree from '@/views/components/depTree'
export default {
  name: 'SysDepartment',
  dicts: [
    'common_status'
  ], // 字典
  components: {
    CrudOpts,
    rrOperation,
    udOperation,
    SysDepartmentInfo,
    SysDepartmentForm,
    DepTree
  },
  cruds() {
    return CRUD({ title: '组织结构', initDataMethod: list, crudMethod: { ...crudApi }, descs: ['paixu', 'id'] })
  },
  mixins: [presenter(), header()],
  data() {
    return {
      highlightDataId: null,
      permission: new Map()
    }
  },
  mounted() {
    this.permission = this.$store.state.menu.buttonPermission.get('SysDep')
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      if (!this.query.parentId) {
        this.query.id = this.query._id
      }
      this.highlightDataId = null
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      /* if (form.parentId === '0') {
        crud.warningNotify('系统内置数据，不能修改')
        return false
      }*/
    },
    [CRUD.HOOK.afterToAdd](crud, form) {
      console.log(this.highlightDataId)
      form.parentId = this.highlightDataId
    },
    [CRUD.HOOK.beforeDelete](crud, form) {
      if (form.parentId === '0') {
        crud.warningNotify('系统内置数据，不能删除')
        return false
      }
    },
    // 加载子节点
    loadChildren(row, treeNode, resolve) {
      // this.query.id = null
      this.query.parentId = row.id
      this.query.id = null // 加载下级的时候去除上级的参数
      list(this.query).then(res => {
        this.query.parentId = null
        if (res.data) {
          resolve(res.data)
        } else {
          resolve([])
        }
      })
    },
    depSearchInput(value) {
      console.log(value)
      if (!value) {
        this.query.parentId = null
        this.query._id = null // 为了避免加载children时造成id参数的交叉感染，故不直接用id
      } else {
        this.query.parentId = null
        this.query._id = value // 为了避免加载children时造成id参数的交叉感染，故不直接用id
      }
    },
    clickLine(row) {
      this.highlightDataId = row.id
    }
  }
}
</script>

<style >
  .vue-treeselect__control{
    height: 30px;
  }
  .vue-treeselect__placeholder, .vue-treeselect__single-value{
    line-height: 28px;
    font-size: small;
  }
  .vue-treeselect__menu{
    font-size: small;
  }
</style>
